﻿// Write your JavaScript code.
$.ajaxSetup({
    beforeSend: function (xhr) {
        if (localStorage.getItem("token") !== null) {
            xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("token"));
        }
    }
});
$.cookie("ip", returnCitySN["cip"]);    // 设置客户端的ip地址到cookie中

// 页面加载的时候, 初始化自动登录
if ($("#login_btns").length > 0) {
    if (localStorage.getItem('token') !== null && localStorage.getItem('token').length > 0) {
        $.ajax({
            url: '/init/login',
            type: 'get',
            error: function (e) {
                console.log(e);
            },
            success: function (json) {
                console.log(json);
                if (json.msg == 'ok') {
                    $.cookie("token", localStorage.getItem('token'));
                    location.reload(true);
                } else {
                    localStorage.removeItem("token");
                }
            }
        })
    }
}

// 点击登录按钮, 如果是QQ登录,需要使用定时函数判断是否登录成功
$("#login_btns").click(function () {
    var timer = setInterval(function () {
        if (localStorage.getItem('token') !== null && localStorage.getItem('token').length > 0) {
            $.ajax({
                url: '/init/login',
                type: 'get',
                error: function (e) {
                    console.log(e);
                },
                success: function (json) {
                    console.log(json);
                    if (json.data == 'login') {
                        $.cookie("token", localStorage.getItem('token'));
                        location.reload(true);
                    }
                }
            })
        }
    }, 1000);
});

// 退出登录
$(".logout_user").click(function () {
    $.ajax({
        url: '/logout',
        type: 'get',
        error: function (e) {
            console.log(e);
        },
        success: function (json) {
            console.log(json);
            if (json.msg == 'ok') {
                $.removeCookie("token");
                localStorage.removeItem("token");
                location.reload(true);
            }
        }
    })
});

// 八字简批提交表单 bzjpForm
function bzjpSub() {
    if ($("#bzjp_date").val() == '') {
        alert("请选择出生时间");
        return;
    }
    $.ajax({
        type: "post",
        url: "/divination/add",
        data: $("#bzjpForm").serialize(),
        error: function (request) {
            alert(request.data);
        },
        success: function (res) {
            // 提交成功之后,等待预测的状态
            $("#bzjp_date").val('')
            alert(res.data);
        }
    });
}